package scene.map.analyster
{
	import scene.map.Map;

	public class SimpleMapAnalyster implements IMapAnalyster
	{
		protected static const COL_SPLIT_CHAR:String = ",";
		
		protected static const ROW_SPLIT_CHAR:String = "\n";
		
		protected static var _instance:SimpleMapAnalyster;
		
		public function SimpleMapAnalyster()
		{
		}
		
		public function analyst(data:String):Map
		{
			if( !data )
			{
				throw Error( "map data string is empty !" );
			}
			
			var map:Map;
			
			try
			{
				var lines:Array = data.split( ROW_SPLIT_CHAR );
				
				var index:int = 0;
				
				var mapBaseInfo:Array = String( lines[index++] ).split( COL_SPLIT_CHAR );
				
				map = new Map();
				
				map.cols = mapBaseInfo[0];
				
				map.rows = mapBaseInfo[1];
				
				map.cells = [];
				
				var cells:Array = map.cells;
				
				for( var i:int = 0; i < lines.length; i++ )
				{
					var line:String = lines[i];
					
					var cols:Array = line.split( COL_SPLIT_CHAR );
					
					var mapRow:Array = cells[i];
					
					if( !mapRow )
					{
						mapRow = [];
						cells[i] = mapRow;
					}
					
					for( var j:int = 0; j < cols.length; j++ )
					{
						mapRow[j] = cols[j];		
					}
				}
				
				
			}
			catch( e:Error )
			{
				throw Error( "map analyst error:\n" + e.getStackTrace() );
			}
			
			return map;
		}
		
		public static function get intance():SimpleMapAnalyster
		{
			if( !_instance )  new SimpleMapAnalyster();
			
			return _instance;
			
		}
	}
}